package com.zhs.gccs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zhs.gccs.R;
import com.zhs.gccs.mapbox.MapLegend;
import com.zhs.gccs.view.taskMap.LegendLayerView;

import java.util.List;

/**
 * Created by 任伟伟
 * Datetime: 2016/11/18-11:07
 * Email: renweiwei@ufashion.com
 */

public class LegendGroupAdapter extends RecyclerView.Adapter<LegendGroupAdapter.ViewHolder> {
    private Context mContext;
    private List<MapLegend> datas = null;

    public LegendGroupAdapter(Context context, List<MapLegend> datas) {
        this.datas = datas;
        this.mContext = context;
    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).
                inflate(R.layout.layout_view_legend_group,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
        MapLegend data = datas.get(position);
        if(data.getGroup()==MapLegend.g_group){
            viewHolder.ll_group.setVisibility(View.VISIBLE);
            viewHolder.rv_legend_children.setVisibility(View.VISIBLE);
            viewHolder.legendLayerView.setVisibility(View.GONE);
            viewHolder.rv_legend_children.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
            LegendGroupAdapter adapter = new LegendGroupAdapter(mContext,data.getChildren());
            viewHolder.rv_legend_children.setAdapter(adapter);
            adapter.setOperationListener(new OperationListener() {
                @Override
                public void onItemClick(MapLegend mapLegendLayer) {
                    if(operationListener!=null)operationListener.onItemClick(mapLegendLayer);
                }
            });
            viewHolder.tv_group_name.setText(data.getGroupName());
            if(data.isOpen()){
                viewHolder.rv_legend_children.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(R.mipmap.icon_jian).into(viewHolder.iv_group);
            }else {
                viewHolder.rv_legend_children.setVisibility(View.GONE);
                Glide.with(mContext).load(R.mipmap.icon_add).into(viewHolder.iv_group);
            }
            viewHolder.ll_group.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if(data.isOpen()){
//                        viewHolder.rv_legend_children.setVisibility(View.GONE);
//                        Glide.with(mContext).load(R.mipmap.jiantou_1).into(viewHolder.iv_group);
//                    }else {
//                        viewHolder.rv_legend_children.setVisibility(View.VISIBLE);
//                        Glide.with(mContext).load(R.mipmap.jiantou_2).into(viewHolder.iv_group);
//                    }
//                    data.setOpen(!data.isOpen());
                    if(operationListener!=null)operationListener.onItemClick(data);
                }
            });
        }else {
            viewHolder.ll_group.setVisibility(View.GONE);
            viewHolder.rv_legend_children.setVisibility(View.GONE);
            viewHolder.legendLayerView.setVisibility(View.VISIBLE);
            viewHolder.legendLayerView.setData(data,position);
            viewHolder.legendLayerView.setOperationListener(new LegendLayerView.OperationListener() {
                @Override
                public void onItemClick() {
                    if(operationListener!=null)operationListener.onItemClick(data);
                }
            });
        }
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas!=null?datas.size():0;
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View ll_group;//
        public ImageView iv_group;
        public TextView tv_group_name;
        public RecyclerView rv_legend_children;
        public LegendLayerView legendLayerView;
        public ViewHolder(View view){
            super(view);
            ll_group =   view.findViewById(R.id.ll_group);
            iv_group =   view.findViewById(R.id.iv_group);
            tv_group_name =   view.findViewById(R.id.tv_group_name);
            rv_legend_children =   view.findViewById(R.id.rv_legend_children);
            legendLayerView =   view.findViewById(R.id.legendLayerView);
        }
    }
    public static interface OperationListener{
        void onItemClick(MapLegend mapLegendLayer);
    }
    private OperationListener operationListener;

    public OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
